#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: LiuHuan
# Datetime: 2019/11/15 16:25


import pymysql

class pySql:
    def __init__(self, ip, user, pwd, db):
        self.ip = ip
        self.user = user
        self.pwd = pwd
        self.db = db
        self.conn = pymysql.connect(host=ip, user=user, password=pwd, db=db, charset='utf8')

    def get_corsor(self):
        return self.conn.cursor()

    def read_sql(self, sql):
        cursor = self.conn.cursor()
        cursor.execute(sql)
        results = cursor.fetchall()
        return results

    def write_sql(self, sql):
        cursor = self.conn.cursor()
        try:
            cursor.execute(sql)
            self.conn.commit()
        except pymysql.Error as e:
            print(e.args[0], e.args[1])
            self.conn.rollback()

    def close(self):
        self.conn.close()

